package com.fwtai.service.api;

import com.fwtai.api.ApiGoodsOrderDao;
import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.pay.wechat.ToolWechat;
import com.fwtai.pay.wechat.WechatPayConstants;
import com.fwtai.quartz.AutoDelivery;
import com.fwtai.quartz.ToolQuartz;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import com.fwtai.tool.ToolOkHttp;
import com.fwtai.tool.ToolString;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 微信支付订单业务层
 * @作者 李宇鹏
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2023-04-11 13:44:32
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class ApiGoodsOrderService{

  private final ApiGoodsOrderDao apiGoodsOrderDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public ApiGoodsOrderService(final ApiGoodsOrderDao apiGoodsOrderDao,final ThreadPoolTaskExecutor taskExecutor){
    this.apiGoodsOrderDao = apiGoodsOrderDao;
    this.taskExecutor = taskExecutor;
  }

  public String add(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.tokenInvalid();
      }
      final PageFormData formData = new PageFormData(request);
      final String p_user_id = "user_id";
      final String p_buyer_type = "buyer_type";
      final String p_openId = "openId";
      final String p_transaction_id = "transaction_id";
      final String p_out_trade_no = "out_trade_no";
      final String p_total_fee = "total_fee";
      final String p_status = "status";
      final String p_evaluate = "evaluate";
      final String p_refund = "refund";
      final String p_version = "version";
      final String p_body = "body";
      final String p_pay_date = "pay_date";
      final String validate = ToolClient.validateField(formData,p_user_id,p_buyer_type,p_openId,p_out_trade_no,p_total_fee,p_status,p_evaluate,p_refund,p_version,p_body);
      if(validate != null){
        return validate;
      }
      final String len_user_id = formData.getString(p_user_id);
      if(len_user_id != null && len_user_id.length() > 19){
        return ToolClient.createJsonFail("购买人|付费用户id字数太多");
      }
      final String len_buyer_type = formData.getString(p_buyer_type);
      if(len_buyer_type != null && len_buyer_type.length() > 1){
        return ToolClient.createJsonFail("购买类型（1是用户购买；2是直营店向区级购买；3区级向总公司购买）字数太多");
      }
      final String len_openId = formData.getString(p_openId);
      if(len_openId != null && len_openId.length() > 40){
        return ToolClient.createJsonFail("微信的openId字数太多");
      }
      final String len_transaction_id = formData.getString(p_transaction_id);
      if(len_transaction_id != null && len_transaction_id.length() > 64){
        return ToolClient.createJsonFail("交易标识码（交易单号）字数太多");
      }
      final String len_out_trade_no = formData.getString(p_out_trade_no);
      if(len_out_trade_no != null && len_out_trade_no.length() > 19){
        return ToolClient.createJsonFail("订单号字数太多");
      }
      final String len_total_fee = formData.getString(p_total_fee);
      if(len_total_fee != null && len_total_fee.length() > 15){
        return ToolClient.createJsonFail("支付金额字数太多");
      }
      final String len_status = formData.getString(p_status);
      if(len_status != null && len_status.length() > 1){
        return ToolClient.createJsonFail("支付状态(0未支付;1已支付)字数太多");
      }
      final String len_evaluate = formData.getString(p_evaluate);
      if(len_evaluate != null && len_evaluate.length() > 1){
        return ToolClient.createJsonFail("评价状态(0待评价;1已评价)字数太多");
      }
      final String len_refund = formData.getString(p_refund);
      if(len_refund != null && len_refund.length() > 1){
        return ToolClient.createJsonFail("退款状态(0未退款;1已退款)字数太多");
      }
      final String len_version = formData.getString(p_version);
      if(len_version != null && len_version.length() > 10){
        return ToolClient.createJsonFail("版本号(乐观锁)字数太多");
      }
      final String len_body = formData.getString(p_body);
      if(len_body != null && len_body.length() > 128){
        return ToolClient.createJsonFail("支付描述（下单描述）字数太多");
      }
      final String len_pay_date = formData.getString(p_pay_date);
      if(len_pay_date != null && len_pay_date.length() > 10){
        return ToolClient.createJsonFail("付款日期（支付成功后更新）字数太多");
      }
      final Long v_user_id = formData.getLong(p_user_id);
      if(v_user_id == null){
        return ToolClient.createJsonFail("购买人|付费用户id不是有效的数据格式");
      }else{
        formData.put(p_user_id,v_user_id);
      }
      final Integer v_buyer_type = formData.getInteger(p_buyer_type);
      if(v_buyer_type == null){
        return ToolClient.createJsonFail("购买类型（1是用户购买；2是直营店向区级购买；3区级向总公司购买）不是有效的数字");
      }else{
        formData.put(p_buyer_type,v_buyer_type);
      }
      final Long v_out_trade_no = formData.getLong(p_out_trade_no);
      if(v_out_trade_no == null){
        return ToolClient.createJsonFail("订单号不是有效的数据格式");
      }else{
        formData.put(p_out_trade_no,v_out_trade_no);
      }
      final BigDecimal v_total_fee = formData.getBigDecimal(p_total_fee);
      if(v_total_fee == null){
        return ToolClient.createJsonFail("支付金额不是有效的数字");
      }else{
        formData.put(p_total_fee,v_total_fee);
      }
      final Integer v_status = formData.getInteger(p_status);
      if(v_status == null){
        return ToolClient.createJsonFail("支付状态(0未支付;1已支付)不是有效的数字");
      }else{
        formData.put(p_status,v_status);
      }
      final Integer v_evaluate = formData.getInteger(p_evaluate);
      if(v_evaluate == null){
        return ToolClient.createJsonFail("评价状态(0待评价;1已评价)不是有效的数字");
      }else{
        formData.put(p_evaluate,v_evaluate);
      }
      final Integer v_refund = formData.getInteger(p_refund);
      if(v_refund == null){
        return ToolClient.createJsonFail("退款状态(0未退款;1已退款)不是有效的数字");
      }else{
        formData.put(p_refund,v_refund);
      }
      final Integer v_version = formData.getInteger(p_version);
      if(v_version == null){
        return ToolClient.createJsonFail("版本号(乐观锁)不是有效的数字");
      }else{
        formData.put(p_version,v_version);
      }
      final Integer v_pay_date = formData.getInteger(p_pay_date);
      if(v_pay_date == null){
        return ToolClient.createJsonFail("付款日期（支付成功后更新）不是有效的数字");
      }else{
        formData.put(p_pay_date,v_pay_date);
      }
      formData.put("kid",new IdBuilder().getId121L());//todo 视情况是否需要处理主键重复的而需要删除所上传的文件,如以用户主键创建的表,可使用唯一键约束并结合IGNORE使用
      return ToolClient.executeRows(apiGoodsOrderDao.add(formData));
    },taskExecutor).join();
  }

  public String edit(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.tokenInvalid();
      }
      final PageFormData formData = new PageFormData(request);
      final String p_kid = "kid";
      final String p_user_id = "user_id";
      final String p_buyer_type = "buyer_type";
      final String p_openId = "openId";
      final String p_transaction_id = "transaction_id";
      final String p_out_trade_no = "out_trade_no";
      final String p_total_fee = "total_fee";
      final String p_status = "status";
      final String p_evaluate = "evaluate";
      final String p_refund = "refund";
      final String p_version = "version";
      final String p_body = "body";
      final String p_pay_date = "pay_date";
      final String validate = ToolClient.validateField(formData,p_kid,p_user_id,p_buyer_type,p_openId,p_out_trade_no,p_total_fee,p_status,p_evaluate,p_refund,p_version,p_body);
      if(validate != null) return validate;
      final String len_user_id = formData.getString(p_user_id);
      if(len_user_id != null && len_user_id.length() > 19){
        return ToolClient.createJsonFail("购买人|付费用户id字数太多");
      }
      final String len_buyer_type = formData.getString(p_buyer_type);
      if(len_buyer_type != null && len_buyer_type.length() > 1){
        return ToolClient.createJsonFail("购买类型（1是用户购买；2是直营店向区级购买；3区级向总公司购买）字数太多");
      }
      final String len_openId = formData.getString(p_openId);
      if(len_openId != null && len_openId.length() > 40){
        return ToolClient.createJsonFail("微信的openId字数太多");
      }
      final String len_transaction_id = formData.getString(p_transaction_id);
      if(len_transaction_id != null && len_transaction_id.length() > 64){
        return ToolClient.createJsonFail("交易标识码（交易单号）字数太多");
      }
      final String len_out_trade_no = formData.getString(p_out_trade_no);
      if(len_out_trade_no != null && len_out_trade_no.length() > 19){
        return ToolClient.createJsonFail("订单号字数太多");
      }
      final String len_total_fee = formData.getString(p_total_fee);
      if(len_total_fee != null && len_total_fee.length() > 15){
        return ToolClient.createJsonFail("支付金额字数太多");
      }
      final String len_status = formData.getString(p_status);
      if(len_status != null && len_status.length() > 1){
        return ToolClient.createJsonFail("支付状态(0未支付;1已支付)字数太多");
      }
      final String len_evaluate = formData.getString(p_evaluate);
      if(len_evaluate != null && len_evaluate.length() > 1){
        return ToolClient.createJsonFail("评价状态(0待评价;1已评价)字数太多");
      }
      final String len_refund = formData.getString(p_refund);
      if(len_refund != null && len_refund.length() > 1){
        return ToolClient.createJsonFail("退款状态(0未退款;1已退款)字数太多");
      }
      final String len_version = formData.getString(p_version);
      if(len_version != null && len_version.length() > 10){
        return ToolClient.createJsonFail("版本号(乐观锁)字数太多");
      }
      final String len_body = formData.getString(p_body);
      if(len_body != null && len_body.length() > 128){
        return ToolClient.createJsonFail("支付描述（下单描述）字数太多");
      }
      final String len_pay_date = formData.getString(p_pay_date);
      if(len_pay_date != null && len_pay_date.length() > 10){
        return ToolClient.createJsonFail("付款日期（支付成功后更新）字数太多");
      }
      final Long v_user_id = formData.getLong(p_user_id);
      if(v_user_id == null){
        return ToolClient.createJsonFail("购买人|付费用户id不是有效的数据格式");
      }else{
        formData.put(p_user_id,v_user_id);
      }
      if(len_buyer_type != null){
        final Integer v_buyer_type = formData.getInteger(p_buyer_type);
        if(v_buyer_type == null){
          return ToolClient.createJsonFail("购买类型（1是用户购买；2是直营店向区级购买；3区级向总公司购买）不是有效的数字");
        }else{
          formData.put(p_buyer_type,v_buyer_type);
        }
      }
      final Long v_out_trade_no = formData.getLong(p_out_trade_no);
      if(v_out_trade_no == null){
        return ToolClient.createJsonFail("订单号不是有效的数据格式");
      }else{
        formData.put(p_out_trade_no,v_out_trade_no);
      }
      final BigDecimal v_total_fee = formData.getBigDecimal(p_total_fee);
      if(v_total_fee == null){
        return ToolClient.createJsonFail("支付金额不是有效的数字");
      }else{
        formData.put(p_total_fee,v_total_fee);
      }
      if(len_status != null){
        final Integer v_status = formData.getInteger(p_status);
        if(v_status == null){
          return ToolClient.createJsonFail("支付状态(0未支付;1已支付)不是有效的数字");
        }else{
          formData.put(p_status,v_status);
        }
      }
      if(len_evaluate != null){
        final Integer v_evaluate = formData.getInteger(p_evaluate);
        if(v_evaluate == null){
          return ToolClient.createJsonFail("评价状态(0待评价;1已评价)不是有效的数字");
        }else{
          formData.put(p_evaluate,v_evaluate);
        }
      }
      if(len_refund != null){
        final Integer v_refund = formData.getInteger(p_refund);
        if(v_refund == null){
          return ToolClient.createJsonFail("退款状态(0未退款;1已退款)不是有效的数字");
        }else{
          formData.put(p_refund,v_refund);
        }
      }
      if(len_version != null){
        final Integer v_version = formData.getInteger(p_version);
        if(v_version == null){
          return ToolClient.createJsonFail("版本号(乐观锁)不是有效的数字");
        }else{
          formData.put(p_version,v_version);
        }
      }
      if(len_pay_date != null){
        final Integer v_pay_date = formData.getInteger(p_pay_date);
        if(v_pay_date == null){
          return ToolClient.createJsonFail("付款日期（支付成功后更新）不是有效的数字");
        }else{
          formData.put(p_pay_date,v_pay_date);
        }
      }
      final Long v = apiGoodsOrderDao.queryExistById(formData.getLong(p_kid));
      if(v == null){
        return ToolClient.createJsonFail("数据不存在,刷新重试");
      }
      formData.put(p_kid,formData.getLong(p_kid));
      return ToolClient.executeRows(apiGoodsOrderDao.edit(formData));
    },taskExecutor).join();
  }

  public String queryById(final Long kid){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      final HashMap<String,Object> map = apiGoodsOrderDao.queryById(kid);
      final List<HashMap<String,Object>> list = apiGoodsOrderDao.getOrderDetails(kid);
      final HashMap<String,Object> result = new HashMap<>(2);
      final Long login = Long.parseLong(loginId);
      final Long payee = (Long)map.get("payee");
      if(payee != null && !payee.equals(login)){
        map.remove("payee");
        map.remove("notes");
      }
      result.put("goodsOrder",map);
      if(list != null){
        result.put("orderDetails",list);
      }
      return ToolClient.queryJson(result);
    },taskExecutor).join();
  }

  public String queryIntegralOrderInfo(final Long kid){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      final HashMap<String,Object> map = apiGoodsOrderDao.queryIntegralOrderInfo(kid);
      final List<HashMap<String,Object>> list = apiGoodsOrderDao.getIntegralDetails(kid);
      final HashMap<String,Object> result = new HashMap<>(2);
      final Long payee = (Long)map.get("payee");
      if(payee != null && !payee.equals(loginId)){
        map.remove("payee");
        map.remove("notes");
      }
      result.put("goodsOrder",map);
      if(list != null){
        result.put("orderDetails",list);
      }
      return ToolClient.queryJson(result);
    },taskExecutor).join();
  }

  public String queryOrderList(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      final PageFormData formData = ToolClient.pageParamsApi(request,"id");
      if(formData == null){
        return ToolClient.jsonValidateField();
      }
      formData.put("loginId",Long.parseLong(loginId));
      return ToolClient.jsonPage(apiGoodsOrderDao.queryOrderList(formData));
    },taskExecutor).join();
  }

  /**适用于api接口,仅含数据不含总记录数总条数,前端仅做返回的数据是否小于每页大小*/
  public String listData(final HttpServletRequest request){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null){
        return ToolClient.jsonValidateField();
      }
      formData.put("loginId",loginId);
      final String status = formData.getString("status");
      if(status != null){
        switch (status){//1待确认收货;2已完成;不传则是全部
          case "1":
            formData.put("cargo_status",1);
            break;
          case "2":
            formData.put("cargo_status",2);
            break;
          default:
            formData.remove("status");
            break;
        }
      }
      return ToolClient.jsonPage(apiGoodsOrderDao.getListData(formData));
    },taskExecutor).join();
  }

  public String listIntegralOrder(final HttpServletRequest request){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null){
        return ToolClient.jsonValidateField();
      }
      final String status = formData.getString("status");
      if(status != null){
        switch (status){//1待确认收货;2已完成;不传则是全部
          case "1":
            formData.put("cargo_status",1);
            break;
          case "2":
            formData.put("cargo_status",2);
            break;
          default:
            formData.remove("status");
            break;
        }
      }
      formData.put("loginId",loginId);
      return ToolClient.jsonPage(apiGoodsOrderDao.listIntegralOrder(formData));
    },taskExecutor).join();
  }

  public String getIntegralList(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null){
        return ToolClient.jsonValidateField();
      }
      formData.put("loginId",Long.parseLong(loginId));
      return ToolClient.jsonPage(apiGoodsOrderDao.getIntegralList(formData));
    },taskExecutor).join();
  }

  public String getIntegralSalesOrder(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null){
        return ToolClient.jsonValidateField();
      }
      formData.put("loginId",Long.parseLong(loginId));
      return ToolClient.jsonPage(apiGoodsOrderDao.getIntegralSalesOrder(formData));
    },taskExecutor).join();
  }

  public String queryAllIntegral(){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.jsonValidateField();
      }
      final List<HashMap<String,Object>> list = apiGoodsOrderDao.getAllIntegral(Long.parseLong(loginId));
      return ToolClient.queryJson(list);
    },taskExecutor).join();
  }

  public String getAllOrder(){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.jsonValidateField();
      }
      final List<HashMap<String,Object>> list = apiGoodsOrderDao.getAllOrder(Long.parseLong(loginId));
      return ToolClient.queryJson(list);
    },taskExecutor).join();
  }

  public String addOrderNotes(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.jsonValidateField();
      }
      final PageFormData formData = new PageFormData(request);
      final String p_kid = "kid";
      final String p_type = "type";
      final String validate = ToolClient.validateField(formData,p_kid,p_type);
      if(validate != null) return validate;
      formData.put("kid",formData.getLong(p_kid));
      formData.put("payee",Long.parseLong(loginId));//收款人
      final Integer type = formData.getInteger(p_type);
      return ToolClient.executeRows(apiGoodsOrderDao.addOrderNotes(formData,type),"恭喜,操作成功","操作失败,稍候重试");
    },taskExecutor).join();
  }

  public String queryIncomeDetails(final Long out_trade_no){
    return CompletableFuture.supplyAsync(()->{
      if(out_trade_no == null){
        return ToolClient.jsonValidateField();
      }
      final Integer payType = apiGoodsOrderDao.queryIncomeOrderType(out_trade_no);
      if(payType == null){
        return ToolClient.createJsonFail("数据不存在,刷新重试");
      }
      final HashMap<String,Object> map = apiGoodsOrderDao.queryIncomeOrder(out_trade_no,payType);
      final List<HashMap<String,Object>> list = apiGoodsOrderDao.getIncomeOrderDetails((Long) map.get("kid"),payType);
      final HashMap<String,Object> result = new HashMap<>(2);
      result.put("goodsOrder",map);
      if(list != null){
        result.put("orderDetails",list);
      }
      return ToolClient.queryJson(result);
    },taskExecutor).join();
  }

  public String queryAllOrderList(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null){
        return ToolClient.jsonValidateField();
      }
      final Long userId = formData.getLong("userId");
      if(userId == null){
        formData.put("userId",loginId);
      }
      return ToolClient.jsonPage(apiGoodsOrderDao.getAllOrderList(formData));
    },taskExecutor).join();
  }

  public String editCargoStatus(final String type,final String out_trade_no){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      if(out_trade_no == null || type == null){
        return ToolClient.jsonValidateField();
      }
      final HashMap<String,Long> params = new HashMap<>(3);
      params.put("loginId",loginId);
      final long outTradeNo = Long.parseLong(out_trade_no);
      params.put("out_trade_no",outTradeNo);
      params.put("clearing_date",System.currentTimeMillis() / 1000);
      final List<HashMap<String,Object>> listAccount = apiGoodsOrderDao.getAccountIn(outTradeNo);
      switch (type){
        case "1":
          if(listAccount.isEmpty()){
            return ToolClient.executeRows(apiGoodsOrderDao.editWechatStatus(params));
          }else{
            return ToolClient.executeRows(apiGoodsOrderDao.editWechatStatus(params,listAccount));
          }
        case "2":
          if(listAccount.isEmpty()){
            return ToolClient.executeRows(apiGoodsOrderDao.editIntegralStatus(params));
          }else{
            return ToolClient.executeRows(apiGoodsOrderDao.editIntegralStatus(params,listAccount));
          }
        default:
          return ToolClient.jsonValidateField();
      }
    },taskExecutor).join();
  }

  /**
   * 处理3天自动收货,自动发货步骤3:处理订单状态业务
   * @param type 1是微信支付的订单;2是积分支付的订单
   * @作者 田应平
   * @QQ 444141300
   * @创建时间 2024/1/26 20:15
  */
  public int editDelivery(final long out_trade_no,final String type){
    final List<HashMap<String,Object>> listAccount = apiGoodsOrderDao.getAccountIn(out_trade_no);
    final HashMap<String,Long> params = new HashMap<>(2);
    params.put("out_trade_no",out_trade_no);
    params.put("clearing_date",System.currentTimeMillis() / 1000);
    switch (type){
      case "1":
        if(listAccount.isEmpty()){
          final int rows = apiGoodsOrderDao.editDeliveryWechat(out_trade_no);
          if(rows > 0){
            ToolQuartz.deleteJob(String.valueOf(out_trade_no),AutoDelivery.groupName);
          }
          return rows;
        }else{
          return apiGoodsOrderDao.editWechatDelivery(out_trade_no,params,listAccount);
        }
      case "2":
        if(listAccount.isEmpty()){
          return apiGoodsOrderDao.editDeliveryIntegral(out_trade_no);
        }else{
          return apiGoodsOrderDao.editIntegralDelivery(out_trade_no,params,listAccount);
        }
      default:
        return 0;
    }
  }

  /**
   * 处理7天自动收货,自动发货步骤3:处理订单状态业务
   * @作者 田应平
   * @QQ 444141300
   * @创建时间 2024/1/26 20:15
  */
  public int editAffirm(final long out_trade_no){
    return apiGoodsOrderDao.editAffirmDelivery(out_trade_no);
  }

  public String getTransactionTrade(final String transaction_id){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      try {
        final String accessToken = ToolWechat.getAccessToken(WechatPayConstants.APP_ID,WechatPayConstants.SECRET);
        if(accessToken == null){
          return ToolClient.jsonValidateField("无效的配置信息");
        }
        final HashMap<String,Object> params = new HashMap<>(1);
        params.put("transaction_id",transaction_id);
        final String url = "https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=" + accessToken;
        final String jsonRequest = ToolOkHttp.postSync(url,ToolClient.toJson(params));
        final HashMap<String,String> map = ToolString.parseJsonObject(jsonRequest);
        if("0".equals(map.get("errcode"))){
          final String order = map.get("order");
          final HashMap<String,String> hashMap = ToolString.parseJsonObject(order);
          final HashMap<String,String> result = new HashMap<>(2);
          result.put("in_complaint",hashMap.get("in_complaint"));
          result.put("order_state",hashMap.get("order_state"));
          return ToolClient.queryJson(result);
        }else{
          return ToolClient.createJsonFail(map.get("errmsg"));
        }
      } catch (final Exception ignored){
        return ToolClient.exceptionJson("数据类型有误");
      }
    },taskExecutor).join();
  }
}